package com.lanhao.aot.aiui.newversion;

import java.util.Map;
import java.util.concurrent.CountDownLatch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StopWatch;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;

import cn.hutool.core.lang.UUID;

public class AIUI45 {
	private static final Logger LOG = LoggerFactory.getLogger(AIUI45.class);
	public static Map<String, AIUIWebSocketClient> socketCacheMap = Maps.newHashMap();
	public static Map<String, Boolean> reauthconnectMap = Maps.newConcurrentMap();

	public static final String UID = "d0b281c17aa24bf0a9597911dca02ff3";  // 用来做上下文交互，标明是同一个人或设备
//	public static final String UID = UUID.fastUUID().toString(true);
	
//	public static final String REQ_TEXT = "请写一篇开学大典演讲报告，不超过100字 ";
//	public static final String REQ_TEXT = "字数太多了，请精简到100字左右 ";
	public static final String REQ_TEXT = "我想听歌";
//	public static final String REQ_TEXT = "今天天气怎么样";
//	public static final String REQ_TEXT = "我想听相声";
//	public static final String REQ_TEXT = "打开空气净化器";
	
	public static void main(String[] args) {
		StopWatch stw = new StopWatch();
		try {
			stw.start();
			LOG.info("调用AIUI开始...");
			AIUIWebSocketClient client = AIUIUtil.getCacheWebSocketClient(UID);
			stw.stop();
			LOG.info("连接AIUI，获取client耗时t={}ms", stw.getTotalTimeMillis());
			stw.start();
			CountDownLatch countDownLatch = client.getCountDownLatch();
			String streamId = UUID.fastUUID().toString(true);
			String requestParam = AIUIUtil.buildCommonIntentParam(UID, streamId, REQ_TEXT);
			LOG.info("向AIUI发送指令，请求参数:{}", requestParam);
			client.send(requestParam);
			AIUIUtil.resultMap.put(streamId, new JSONObject());
			// 等待交互完成
			countDownLatch.await();
			JSONObject resultObject = AIUIUtil.resultMap.get(streamId);
			LOG.info("调用AIUI返回信息resultObject={}", resultObject.toString());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			stw.stop();
			long totalTimeMillis = stw.getTotalTimeMillis();
			LOG.info("调用AIUI结束，总耗时:totalTime={}ms", totalTimeMillis);
		}
	}

}
